import db from "../models/index.js";

let user = db.user;
let userDao = {};

// 添加新用户数据
userDao.addUser = async (user_id, email, gupiao_id, t) => {
  if (!t) {
    const [userIns, create] = await user.findOrCreate({
      where: {user_id, email, gupiao_id}
    });
    return userIns;
  }
  const [userIns, create] = await user.findOrCreate({
    where: {user_id, email, gupiao_id},
    transaction: t
  });
  return userIns;
};

// 获取用户邮箱
userDao.getUserEmailWithGuPiaoId = (user_id, gupiao_id) => {
  return user.findOne({
    where: {user_id, gupiao_id},
    raw: true
  });
};

// 更新用户邮箱
userDao.updateUserEmail = (user_id, email, gupiao_id) => {
  return user.update({email}, {
    where: {user_id, gupiao_id}
  });
};

// 更新下次发送时间
userDao.updateUserNexttime = (user_id, nexttime, gupiao_id) => {
  return user.update({nexttime}, {
    where: {user_id, gupiao_id}
  });
};
export default userDao;
